import { getWishList as fetchWishList, getWishDetail as fetchWishDetail, createWish as apiCreateWish, updateWish as apiUpdateWish, deleteWish as apiDeleteWish } from '@/api/wish'

export default {
  namespaced: true,
  state: {
    wishList: [],
    total: 0,
    currentWish: null,
    loading: false,
    filters: {
      status: -1, // -1: 全部, 0: 未开始, 1: 进行中, 2: 已实现
      keyword: ''
    },
    pagination: {
      page: 1,
      pageSize: 10,
      total: 0
    }
  },
  mutations: {
    SET_WISH_LIST(state, list) {
      state.wishList = list
    },
    ADD_WISH_LIST(state, list) {
      state.wishList = [...state.wishList, ...list]
    },
    SET_TOTAL(state, total) {
      state.total = total
    },
    SET_CURRENT_WISH(state, wish) {
      state.currentWish = wish
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_FILTERS(state, filters) {
      state.filters = { ...state.filters, ...filters }
    },
    SET_PAGINATION(state, pagination) {
      state.pagination = { ...state.pagination, ...pagination }
    },
    RESET_FILTERS(state) {
      state.filters = {
        status: -1,
        keyword: ''
      }
      state.pagination.page = 1
    },
    UPDATE_WISH_IN_LIST(state, updatedWish) {
      const index = state.wishList.findIndex(item => item.id === updatedWish.id)
      if (index !== -1) {
        state.wishList.splice(index, 1, updatedWish)
      }
    },
    REMOVE_WISH_FROM_LIST(state, id) {
      const index = state.wishList.findIndex(item => item.id === id)
      if (index !== -1) {
        state.wishList.splice(index, 1)
      }
    },
    UPDATE_WISH_PROGRESS(state, { id, progress }) {
      if (state.currentWish && state.currentWish.id === id) {
        state.currentWish.progress = progress
      }
      
      const index = state.wishList.findIndex(item => item.id === id)
      if (index !== -1) {
        state.wishList[index].progress = progress
      }
    }
  },
  actions: {
    // 获取愿望列表
    async getWishList({ commit }, { page = 1, limit = 10, status = null, keyword = '' } = {}) {
      try {
        const params = { page, limit }
        
        // 只有当 status 不为 null 且不为 0 时才添加到参数中
        if (status !== null && status !== 0) {
          // 确保 status 是数字类型
          const statusValue = parseInt(status)
          if (!isNaN(statusValue) && statusValue > 0 && statusValue <= 3) {
            params.status = statusValue - 1
          }
        }
        
        // 添加关键词搜索
        if (keyword && keyword.trim()) {
          params.keyword = keyword.trim()
        }
        
        console.log('请求参数:', params)
        const res = await fetchWishList(params)
        
        if (res) {
          commit('SET_WISH_LIST', res.records || [])
          commit('SET_TOTAL', res.total || 0)
        }
        return res
      } catch (error) {
        console.error('获取愿望列表失败', error)
        throw error
      }
    },
    
    // 加载更多愿望
    async loadMore({ commit, state }, { page = 1, limit = 10, status = null, keyword = '' } = {}) {
      try {
        const params = { page, limit }
        
        if (status !== null && status !== 0) {
          const statusValue = parseInt(status)
          if (!isNaN(statusValue) && statusValue > 0 && statusValue <= 3) {
            params.status = statusValue - 1
          }
        }
        
        // 添加关键词搜索
        if (keyword && keyword.trim()) {
          params.keyword = keyword.trim()
        }
        
        const res = await fetchWishList(params)
        
        if (res) {
          commit('ADD_WISH_LIST', res.records || [])
          commit('SET_TOTAL', res.total || 0)
        }
        return res
      } catch (error) {
        console.error('加载更多愿望失败', error)
        throw error
      }
    },
    
    // 获取愿望详情
    async getWishDetail({ commit }, id) {
      try {
        const res = await fetchWishDetail(id)
        if (res) {
          commit('SET_CURRENT_WISH', res)
        }
        return res
      } catch (error) {
        console.error('获取愿望详情失败', error)
        throw error
      }
    },
    
    // 创建愿望
    async createWish({ dispatch }, wishData) {
      try {
        const res = await apiCreateWish(wishData)
        return res
      } catch (error) {
        console.error('创建愿望失败', error)
        throw error
      }
    },
    
    // 更新愿望
    async updateWish({ commit, state }, wishData) {
      try {
        const res = await apiUpdateWish(wishData)
        if (res) {
          // 更新当前愿望
          commit('SET_CURRENT_WISH', { ...state.currentWish, ...wishData })
        }
        return res
      } catch (error) {
        console.error('更新愿望失败', error)
        throw error
      }
    },
    
    // 更新愿望进度
    async updateWishProgress({ commit }, { id, progress }) {
      try {
        const res = await apiUpdateWish({ id, progress })
        if (res) {
          commit('UPDATE_WISH_PROGRESS', { id, progress })
        }
        return res
      } catch (error) {
        console.error('更新愿望进度失败', error)
        throw error
      }
    },
    
    // 删除愿望
    async deleteWish({ commit }, id) {
      try {
        const res = await apiDeleteWish(id)
        return res
      } catch (error) {
        console.error('删除愿望失败', error)
        throw error
      }
    },
    
    // 设置筛选条件
    setFilters({ commit, dispatch }, filters) {
      commit('SET_FILTERS', filters)
      commit('SET_PAGINATION', { page: 1 })
      return dispatch('getWishList')
    },
    
    // 重置筛选条件
    resetFilters({ commit, dispatch }) {
      commit('RESET_FILTERS')
      return dispatch('getWishList')
    }
  },
  getters: {
    wishList: state => state.wishList,
    total: state => state.total,
    currentWish: state => state.currentWish,
    loading: state => state.loading,
    filters: state => state.filters,
    pagination: state => state.pagination,
    hasMore: state => state.wishList.length < state.pagination.total
  }
} 